package cn.supermaster.management.web.service.system.impl;

import cn.supermaster.management.web.service.system.ISCaptchaService;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.service.system.impl
 * @ClassName: SCaptchaServiceImpl
 * @Description: [验证码生成]
 * <p> 图片验证码（支持算术形式） </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/10 20:16
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class SCaptchaServiceImpl implements ISCaptchaService {


    @Resource(name = "captchaProducer")
    private Producer captchaProducer;

    @Resource(name = "captchaProducerMath")
    private Producer captchaProducerMath;

    @Override
    public ModelAndView doCreateKaptchaImage(HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            HttpSession session = request.getSession();
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
            response.setHeader("Pragma", "no-cache");
            response.setContentType("image/jpeg");
            ////
            String type = request.getParameter("type");
            String captchaText = null;
            String code = null;
            BufferedImage bufferedImage = null;
            if ("math".equals(type)) {
                String capText = captchaProducerMath.createText();
                captchaText = capText.substring(0, capText.lastIndexOf("@"));
                code = capText.substring(capText.lastIndexOf("@") + 1);
                bufferedImage = captchaProducerMath.createImage(captchaText);
            } else if ("char".equals(type)) {
                captchaText = code = captchaProducer.createText();
                bufferedImage = captchaProducer.createImage(captchaText);
            }
            session.setAttribute(Constants.KAPTCHA_SESSION_KEY, code);

            outputStream = response.getOutputStream();
            ImageIO.write(bufferedImage, "jpg", outputStream);
            outputStream.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            doCloseResource(outputStream);
        }
        return null;
    }

    /**
     * @Description: [创建数字验证码]
     * <p></P>
     * @Title: createMathKaptcha
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: session
     * @Return: java.awt.image.BufferedImage
     */
    private BufferedImage createMathKaptcha(HttpSession session) {
        //
        String capText = captchaProducerMath.createText();
        String captchaText = capText.substring(0, capText.lastIndexOf("@"));
        String code = capText.substring(capText.lastIndexOf("@") + 1);
        BufferedImage bufferedImage = captchaProducerMath.createImage(captchaText);

        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, code);
        return bufferedImage;
    }

    /**
     * @Description: [创建字符串验证码]
     * <p></P>
     * @Title: createCharKaptcha
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: session
     * @Return: java.awt.image.BufferedImage
     */
    private BufferedImage createCharKaptcha(HttpSession session) {
        BufferedImage bufferedImage = null;
        //
        String captchaText, code;
        //
        captchaText = code = captchaProducer.createText();
        bufferedImage = captchaProducer.createImage(captchaText);

        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, code);
        return bufferedImage;
    }

    /**
     * @Description: [关闭资源]
     * <p></P>
     * @Title: doCloseResource
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: out
     * @Return: void
     * @Throws:
     */
    private void doCloseResource(OutputStream outputStream) {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}